<template>
    <div class="box">
        <h1 style="text-align: center;">分类列表</h1>
        <br>
        <el-button type="primary" @click="handlerAdd" v-permission="{ path: '/cat/add', name: '分类添加' }">添加分类</el-button>
        <el-table :data="cats" height="250" border style="width: 100%">
            <el-table-column prop="catname" label="分类名称"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
                        v-permission="{ path: '/cat/edit', name: '分类修改' }">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)"
                        v-permission="{ path: '/cat/del', name: '分类删除' }">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 新增及编辑 -->
        <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="分类名称">
                    <el-input v-model="form.catname"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="close">取 消</el-button>
                <el-button type="primary" @click="ADDorEDIT">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { catList, catAdd, catEdit, catDel } from '@/request';
import permission from '@/utils/permission'

export default {
    directives: {
        permission
    },
    data() {
        return {
            cats: [],
            dialogVisible: false,
            form: {
                catname: ''
            },
            status: false
        }
    },
    methods: {
        handleClose() {
            this.dialogVisible = false;
            this.form.catname = '';
            delete this.form.id
        },
        close() {
            this.dialogVisible = false;
            this.form.catname = '';
            delete this.form.id
        },
        handlerAdd() {
            this.status = false;
            this.dialogVisible = true
        },
        ADDorEDIT() {
            if (!this.status) {
                this.ADD()
                return
            }
            this.EDIT()
        },
        async ADD() {
            let data = await catAdd(this.form)
            console.log(data);
            if (data.error_code === 200) {
                this.$message({
                    message: data.msg,
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                        this.getCatList()
                        this.dialogVisible = false
                        this.form.catname = ''
                    }
                });
            } else {
                this.$message({
                    message: data.msg,
                    type: 'error',
                });
            }
        },
        async EDIT() {
            let data = await catEdit(this.form)
            console.log(data);
            if (data.error_code === 200) {
                this.$message({
                    message: data.msg,
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                        this.getCatList()
                        this.dialogVisible = false
                        this.form.catname = ''
                        delete this.form.id
                    }
                });
            } else {
                this.$message({
                    message: data.msg,
                    type: 'error',
                });
            }

        },
        async getCatList() {
            let data = await catList()
            console.log(data);
            if (data.error_code === 200) {
                this.cats = data.data.catlist

            }
        },
        handleEdit(index, row) {
            // console.log(index, row);
            this.status = true;
            this.form.catname = row.catname
            this.form.id = row._id
            this.dialogVisible = true
        },
        handleDelete(index, row) {
            // console.log(index, row);
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                catDel(row._id).then(data => {
                    if (data.error_code === 200) {
                        this.$message({
                            message: data.msg,
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.cats.splice(index, 1)
                            }
                        });
                    } else {
                        this.$message({
                            message: data.msg,
                            type: 'error',
                        });
                    }
                })
            })
        }
    },
    created() {
        this.getCatList()
    }
}
</script>

<style>
.el-table,
.box {
    height: 90% !important
}
</style>